<template>
  <el-dialog title="加工作业详情" :visible.sync="showDialog" top="0" :close-on-click-modal="false" width="90%" @close="cancel">
    <template v-if="selectedItem !== null">
      <el-divider content-position="left">
        <span>所属批次详情</span>
      </el-divider>
      <el-descriptions :column="2" border>
        <el-descriptions-item>
          <template slot="label">
            批次名称
          </template>
          {{ selectedItem.batchName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            批次号
          </template>
          {{ selectedItem.batchNo }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            加工产品
          </template>
          {{ selectedItem.product.productName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            加工类型：
          </template>
          {{ selectedItem.batchType === 'SELF' ? '自主生产' : '委托加工' }}
        </el-descriptions-item>
      </el-descriptions>
      <el-divider content-position="left">
        <span>加工作业详情</span>
      </el-divider>
      <el-descriptions :column="2" border>
        <el-descriptions-item>
          <template slot="label">
            加工作业名称
          </template>
          {{ selectedItem.taskName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            加工作业状态
          </template>
          {{ statusMap.find(i => i.status === selectedItem.status).name }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            预期开始时间
          </template>
          {{ selectedItem.startTime | formateTime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            预期结束时间
          </template>
          {{ selectedItem.endTime | formateTime }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            关联产线
          </template>
          {{ selectedItem.productLineName }}
        </el-descriptions-item>
        <el-descriptions-item v-if="selectedItem.status === 'PROGRESS'">
          <template slot="label">
            是否逾期
          </template>
          {{ selectedItem.overdue ? '是' : '否' }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            作业人
          </template>
          {{ selectedItem.processName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            操作人
          </template>
          {{ selectedItem.operatorName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            预估加工量
          </template>
          {{ selectedItem.processingVolume }} {{ selectedItem.product.unitName }}
        </el-descriptions-item>
        <el-descriptions-item v-if="selectedItem.status === 'PROGRESS'">
          <template slot="label">
            当前已加工量
          </template>
          {{ num }} {{ selectedItem.product.unitName }}
        </el-descriptions-item>
        <template v-if="['SUSPENDED', 'COMPLETED'].includes(selectedItem.status)">
          <el-descriptions-item>
            <template slot="label">
              实际加工量
            </template>
            {{ selectedItem.actualVolume }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              产品报废量
            </template>
            {{ selectedItem.scrapNum }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              实际开始时间
            </template>
            {{ selectedItem.actualStartTime | formateTime }}
          </el-descriptions-item>
        </template>
        <template v-if="selectedItem.status === 'COMPLETED'">
          <el-descriptions-item>
            <template slot="label">
              实际结束时间
            </template>
            {{ selectedItem.actualEndTime | formateTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              是否按期完成
            </template>
            {{ selectedItem.overdue ? '否' : '是' }}
          </el-descriptions-item>
        </template>
        <template v-if="selectedItem.status === 'SUSPENDED'">
          <el-descriptions-item>
            <template slot="label">
              实际中止时间
            </template>
            {{ selectedItem.suspendEndTime | formateTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              中止原因
            </template>
            {{ selectedItem.suspendReason }}
          </el-descriptions-item>
        </template>
      </el-descriptions>
      <div v-if="['SUSPENDED', 'COMPLETED'].includes(selectedItem.status)" class="other">
        <div class="other-info">
          <p class="smallp">实际附属品量</p>
          <el-descriptions :column="2" border>
            <el-descriptions-item v-for="(item, index) in selectedItem.taskAccessoryList" :key="index">
              <template slot="label"> {{ item.materialName }} </template>
              {{ item.num }} {{ item.unitName }}
            </el-descriptions-item>
          </el-descriptions>
        </div>
        <div class="other-info">
          <p class="smallp">剩余原料量</p>
          <el-descriptions :column="2" border>
            <el-descriptions-item v-for="(item, index) in selectedItem.taskRawMaterialList" :key="index">
              <template slot="label">
                {{ item.materialName }}
              </template>
              {{ item.surplusNum }} {{ item.unitName }}
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
      <el-divider content-position="left">
        <span class="pointer" @click="more = !more">{{ more ? '收起工艺及设备' : '显示工艺及设备' }}</span>
      </el-divider>
      <template v-if="more">
        <div class="other">
          <div class="other-info">
            <p>加工产品工艺</p>
            <p class="smallp">{{ selectedItem.product.productName }}</p>
            <el-descriptions :column="2" border>
              <el-descriptions-item v-for="(item, index) in processList" :key="index">
                <template slot="label"> 工序{{ item.number }} </template>
                {{ item.processesName }}
              </el-descriptions-item>
            </el-descriptions>
          </div>
          <div class="other-info">
            <p>加工设备</p>
            <p class="smallp">{{ selectedItem.productLineName }}</p>
            <el-descriptions :column="2" border>
              <el-descriptions-item v-for="(item, index) in selectedItem.equipmentList" :key="index">
                <template slot="label">
                  {{ item.equipmentTypeName }}
                </template>
                {{ item.equipmentName }}
              </el-descriptions-item>
            </el-descriptions>
          </div>
        </div>
      </template>
    </template>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" class="savebtn" @click="cancel()">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import moment from 'moment';
import apiProcess from '@/api/processing-management/basic-info-manage/product-process';
import api from '@/api/processing-management/process-task-manage/index';
export default {
  filters: {
    formateTime: function(value) {
      if (!value) {
        return '';
      }
      return moment(value).format('YYYY-MM-DD HH:mm:ss');
    }
  },
  props: {
    value: Boolean,
    selectedItem: {
      type: Object,
      default: null
    },
    statusMap: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      showDialog: false,
      more: false,
      processList: [],
      num: ''
    };
  },
  watch: {
    value(v) {
      this.showDialog = v;
      const { selectedItem } = this;
      if (v) {
        this.getProcess();
        if (!selectedItem) return;
        if (selectedItem.status === 'PROGRESS') {
          this.getTaskProcess();
        }
      }
    }
  },
  mounted() {},
  methods: {
    reset() {
      this.processList = [];
      this.more = false;
      this.num = '';
    },
    getProcess() {
      const { selectedItem } = this;
      if (selectedItem === null) return;
      apiProcess
        .getProcessesListByCond({
          productId: selectedItem.productId
        })
        .then(res => {
          if (res.code === 1) {
            this.processList = res.data;
          }
        });
    },
    getTaskProcess() {
      const { selectedItem } = this;
      api.getTaskProgress({ taskId: selectedItem.taskId }).then(res => {
        if (res.code === 1) {
          const list = res.data.reverse();
          if (list.length !== 0) {
            this.num = list[0].num;
          }
        }
      });
    },
    close() {
      setTimeout(() => {
        this.reset();
      }, 250);
      this.$emit('input', false);
    },
    cancel() {
      setTimeout(() => {
        this.reset();
      }, 250);
      this.$emit('input', false);
    }
  }
};
</script>

<style lang="scss" scoped>
p {
  font-size: 16px;
  font-weight: bold;
}
::v-deep {
  .el-divider__text {
    background-color: #f0f3f7;
  }
}
::v-deep {
  .el-descriptions .is-bordered .el-descriptions-item__cell {
    border-color: #000;
    color: #000;
  }
  .el-descriptions-item__label {
    font-weight: bold !important;
  }
}
.other {
  display: flex;
  justify-content: space-between;
  .other-info {
    flex: 0 0 49%;
  }
}
.smallp {
  font-size: 14px;
}
</style>
